# -*- encoding:utf-8 -*-
"""
显示字库文件中所有字符
"""
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QVBoxLayout, QWidget, QLabel, QLineEdit, QScrollArea
from PySide6.QtGui import QPixmap, QImage, QBitmap, QPainter, QImageWriter
from PySide6.QtCore import QByteArray, Qt, QSize


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        with open(r'..\data\FONT.ROM', 'rb') as f:
            self.font_data = f.read()
        label_width, label_height = 98*16, 180*16
        scrollArea = QScrollArea(self)
        scrollArea.setMinimumWidth(label_width+20)
        label = QLabel(scrollArea)
        label.setGeometry(0, 0, label_width, label_height)
        scrollArea.setWidget(label)
        self.setCentralWidget(scrollArea)
        self.setGeometry(0, 0, 800, 600)

        image = QImage(label_width, label_height, QImage.Format_Mono)
        image.fill(1)
        painter = QPainter(image)
        offset = 0x000000
        print(f'0x{offset:06X}')
        for ch1 in range(0xA1, 0xFF):
            painter.drawText(16+(ch1-0xA1)*16, 12, f'{ch1:02X}')
        for ch0 in range(0xB0, 0xF8):
            painter.drawText(0, 12+16+(ch0-0xB0)*16, f'{ch0:02X}')
            for ch1 in range(0xA1, 0xFF):
                bitmap = QBitmap.fromData(QSize(16, 16), self.font_data[offset:offset+32], QImage.Format_Mono)
                painter.drawPixmap(16+(ch1-0xA1)*16, 16+(ch0-0xB0)*16, QPixmap(bitmap))
                offset += 32
        print(f'0x{offset:06X}')
        for ch1 in range(0xA1, 0xFF):
            painter.drawText(16+(ch1-0xA1)*16, 12+73*16, f'{ch1:02X}')
        for ch0 in range(0xA1, 0xAA):
            painter.drawText(0, 12+73*16+16+(ch0-0xA1)*16, f'{ch0:02X}')
            for ch1 in range(0xA1, 0xFF):
                bitmap = QBitmap.fromData(QSize(16, 16), self.font_data[offset:offset+32], QImage.Format_Mono)
                painter.drawPixmap(16+(ch1-0xA1)*16, 73*16+16+(ch0-0xA1)*16, QPixmap(bitmap))
                offset += 32
        offset += 0x5C0  # 32*46
        print(f'0x{offset:06X}')
        for ch1 in range(0x40, 0xA1):
            painter.drawText(16+(ch1-0x40)*16, 12+83*16, f'{ch1:02X}')
        for ch0 in range(0xA8, 0xAA):
            painter.drawText(0, 12+83*16+16+(ch0-0xA8)*16, f'{ch0:02X}')
            for ch1 in range(0x40, 0xA1):
                if ch1 == 0x7F:
                    continue
                bitmap = QBitmap.fromData(QSize(16, 16), self.font_data[offset:offset+32], QImage.Format_Mono)
                painter.drawPixmap(16+(ch1-0x40)*16, 83*16+16+(ch0-0xA8)*16, QPixmap(bitmap))
                offset += 32
        offset += 0x1C0  # 32*14
        print(f'0x{offset:06X}')
        for ch1 in range(0x00, 0x80):
            bitmap = QBitmap.fromData(QSize(8, 16), self.font_data[offset:offset+16], QImage.Format_Mono)
            painter.drawPixmap(16+ch1*8, 86*16+16, QPixmap(bitmap))
            offset += 16
        print(f'0x{offset:06X}')
        for ch1 in range(0xA1, 0xFF):
            painter.drawText(16+(ch1-0xA1)*16, 12+88*16, f'{ch1:02X}')
        for ch0 in range(0xAA, 0xB0):
            painter.drawText(0, 12+88*16+16+(ch0-0xAA)*16, f'{ch0:02X}')
            for ch1 in range(0xA1, 0xFF):
                bitmap = QBitmap.fromData(QSize(16, 16), self.font_data[offset:offset+32], QImage.Format_Mono)
                painter.drawPixmap(16+(ch1-0xA1)*16, 88*16+16+(ch0-0xAA)*16, QPixmap(bitmap))
                offset += 32
        print(f'0x{offset:06X}')
        for ch1 in range(0xA1, 0xFF):
            painter.drawText(16+(ch1-0xA1)*16, 12+95*16, f'{ch1:02X}')
        for ch0 in range(0xF8, 0xFF):
            painter.drawText(0, 12+95*16+16+(ch0-0xF8)*16, f'{ch0:02X}')
            for ch1 in range(0xA1, 0xFF):
                bitmap = QBitmap.fromData(QSize(16, 16), self.font_data[offset:offset+32], QImage.Format_Mono)
                painter.drawPixmap(16+(ch1-0xA1)*16, 95*16+16+(ch0-0xF8)*16, QPixmap(bitmap))
                offset += 32
        print(f'0x{offset:06X}')
        for ch1 in range(0x40, 0xA1):
            painter.drawText(16+(ch1-0x40)*16, 12+103*16, f'{ch1:02X}')
        for ch0 in range(0xA1, 0xA8):
            painter.drawText(0, 12+103*16+16+(ch0-0xA1)*16, f'{ch0:02X}')
            for ch1 in range(0x40, 0xA1):
                bitmap = QBitmap.fromData(QSize(16, 16), self.font_data[offset:offset+32], QImage.Format_Mono)
                painter.drawPixmap(16+(ch1-0x40)*16, 103*16+16+(ch0-0xA1)*16, QPixmap(bitmap))
                offset += 32
        print(f'end: 0x{offset:06X}')

        offset = 0x08B363
        print(f'0x{offset:06X}')
        for ch1 in range(0x00, 0x80):
            bitmap = QBitmap.fromData(QSize(8, 16), self.font_data[offset:offset+16], QImage.Format_Mono)
            painter.drawPixmap(16+ch1*8, 111*16+16, QPixmap(bitmap))
            offset += 16
        offset = 0x08B363+0x810
        print(f'0x{offset:06X}')
        for ch1 in range(0x00, 0x80):
            bitmap = QBitmap.fromData(QSize(8, 16), self.font_data[offset:offset+16], QImage.Format_Mono)
            painter.drawPixmap(16+ch1*8, 112*16+16, QPixmap(bitmap))
            offset += 16
        offset = 0x08B363+0x810+0x810
        print(f'0x{offset:06X}')
        for ch1 in range(0x00, 0x80):
            bitmap = QBitmap.fromData(QSize(8, 16), self.font_data[offset:offset+16], QImage.Format_Mono)
            painter.drawPixmap(16+ch1*8, 113*16+16, QPixmap(bitmap))
            offset += 16
        offset = 0x08B363+0x810+0x810+0x810
        print(f'0x{offset:06X}')
        for ch1 in range(0x00, 0x80):
            bitmap = QBitmap.fromData(QSize(8, 16), self.font_data[offset:offset+16], QImage.Format_Mono)
            painter.drawPixmap(16+ch1*8, 114*16+16, QPixmap(bitmap))
            offset += 16
        print(f'0x{offset:06X}')
        offset = 0x0008D3A3
        print(f'0x{offset:06X}')
        for ch1 in range(0x40, 0xA1):
            painter.drawText(16+(ch1-0x40)*16, 12+116*16, f'{ch1:02X}')
        for ch0 in range(0xAA, 0xDA):
            painter.drawText(0, 12+116*16+16+(ch0-0xAA)*16, f'{ch0:02X}')
            for ch1 in range(0x40, 0xA1):
                bitmap = QBitmap.fromData(QSize(16, 16), self.font_data[offset:offset+32], QImage.Format_Mono)
                painter.drawPixmap(16+(ch1-0x40)*16, 116*16+16+(ch0-0xAA)*16, QPixmap(bitmap))
                offset += 32
        print(f'0x{offset:06X}')
        painter.end()
        label.setPixmap(QPixmap(image))

    def displayAscii(self, offset):
        bitmap = QBitmap.fromData(QSize(8, 16), self.font_data[offset:offset+16], QImage.Format_Mono)
        self.display.setPixmap(QPixmap(bitmap))

    def displayChinese(self, offset):
        bitmap = QBitmap.fromData(QSize(16, 16), self.font_data[offset:offset+32], QImage.Format_Mono)
        self.display.setPixmap(QPixmap(bitmap))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())